import storage from './storage'

export const Storage = {
  readStorage(key, defaultValue) {
    // console.log(`Read storage : ${key}`)
    let expired = storage.readObject('[EXPIRED]' + key)
    expired = expired ? parseInt(expired) : 0
    // 已过期
    if (expired > 0 && new Date().getTime() / 1000 > expired) {
      return defaultValue
    }

    let readValue = storage.readObject(key)
    return readValue !== null && readValue !== undefined ? readValue : defaultValue
  },
  writeStorage(key, object, duration) {
    // console.log(`Write storage : ${key}`)
    duration = parseInt(duration)
    if (duration > 0) {
      let expired = new Date().getTime() / 1000 + duration
      storage.writeObject('[EXPIRED]' + key, expired)
    }
    return storage.writeObject(key, object)
  }
}

export const StorageKeys = {
  UserInfo: 'LocalStorage:UserInfo',
  Designers: 'LocalStorage:Designers'
}

export const isArray = function (object) {
  let gettype = Object.prototype.toString
  return gettype.call(object) === '[object Array]'
}
